<div id="paperScore" style="color:red;text-align: center;font-size: 24px;">
</div>
<div  id="resultView">
</div>
<script>
    layui.use(['index',  'config', 'admin', 'layer'], function () {
        var admin=layui.admin;
        var layer=layui.layer;
        var index=layui.index;
        //当前用户id
        // let userid;
        // index.getUser(function (user) {
        //     userid=user.id;
        // });
        //试卷数据
        var tempData = admin.getTempData("paper_data");
        admin.req("api-exam/exam/findResult",{expaid:tempData.expaid,userid:tempData.userid},function (res) {
            let data=res.data;
            $("#paperScore").html("得分："+res.count);
            generatorHtml(data);
        },"get");
        //生成html
        function generatorHtml(data){
            let html1 = choice(data);
            let html2 = notChoice(data);
            $("#resultView").html(html1+html2);
        }
        //选择题
        function choice(data) {
            let f=true;
            let html='<div style="font-weight: bold">一、选择题</div>\n';
            let num=1;
            for (let i = 0; i < data.length; i++) {
                if(data[i].tpname=='选择题'){
                    html += '<div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">'+num+'、('+data[i].score+'分)</label>\n' +
                        '    <div class="layui-input-block" contenteditable="true">\n' +
                        '        <textarea  class="layui-textarea"  autoHeight="true" readonly>'+data[i].qtitle+'</textarea>\n' +
                        '    </div>\n' +
                        '</div>\n' +
                        '<div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">A、</label>\n' +
                        '    <div class="layui-input-inline">\n' +
                        '        <input class="layui-input" value="'+data[i].optioA+'" readonly >\n' +
                        '    </div>\n' +
                        '    <label class="layui-form-label">B、</label>\n' +
                        '    <div class="layui-input-inline">\n' +
                        '        <input  class="layui-input" value="'+data[i].optioB+'" readonly >\n' +
                        '    </div>\n' +
                        '    <label class="layui-form-label">C、</label>\n' +
                        '    <div class="layui-input-inline">\n' +
                        '        <input class="layui-input" value="'+data[i].optioC+'" readonly >\n' +
                        '    </div>\n' +
                        '    <label class="layui-form-label">D、</label>\n' +
                        '    <div class="layui-input-inline">\n' +
                        '        <input  class="layui-input" value="'+data[i].optioD+'" readonly >\n' +
                        '    </div>\n' +
                        '</div>';
                        html+='<div class="layui-form-item ">\n' +
                        '    <label class="layui-form-label">答案</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '        <input class="layui-input " value="'+data[i].answer+'" readonly >\n' +
                        '    </div>\n' +
                        '</div>';
                    if(data[i].result==2){
                        html+='<div class="layui-form-item">\n' +
                            '    <label class="layui-form-label">结果</label>\n' +
                            '    <div class="layui-input-block">\n' +
                            '        <span class="layui-badge layui-bg-red">错误</span>\n' +
                            '    </div>\n' +
                            '</div>';
                    }else if(data[i].result==1) {
                        html+='<div class="layui-form-item">\n' +
                            '    <label class="layui-form-label">结果</label>\n' +
                            '    <div class="layui-input-block">\n' +
                            '        <span class="layui-badge layui-bg-green">正确</span>\n' +
                            '    </div>\n' +
                            '</div>';
                    }
                    html+='<div class="layui-form-item layui-bg-gray">\n' +
                        '    <label class="layui-form-label">参考答案</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '        <input class="layui-input layui-bg-gray" value="'+data[i].standard+'" readonly >\n' +
                        '    </div>\n' +
                        '</div>'
                    ;
                    num++;
                    f=false;
                }
            }
            html+=''
            if(f){
                html='';
            }
            return html;
        }
        //非选择题html
        function notChoice(data) {
            let flag=true;
            let html='<div style="font-weight: bold">二、问答题</div>\n';
            let num=1;
            for (let i = 0; i < data.length; i++) {
                if(data[i].tpname!="选择题"){
                    html += '<div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">'+num+'、('+data[i].score+'分) </label>\n' +
                        '    <div class="layui-input-block" contenteditable="true">\n' +
                        '        <textarea  class="layui-textarea"  autoHeight="true" readonly>'+data[i].qtitle+'</textarea>\n' +
                        '    </div>\n' +
                        '</div>\n' +
                        '<div class="layui-form-item ">\n' +
                        '    <label class="layui-form-label">答案</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '        <textarea  class="layui-textarea "  autoHeight="true" readonly>'+data[i].answer+'</textarea>\n' +
                        '    </div>\n' +
                        '</div>';

                    if(data[i].result==2){
                        html+='<div class="layui-form-item">\n' +
                            '    <label class="layui-form-label">结果</label>\n' +
                            '    <div class="layui-input-block">\n' +
                            '        <input class="layui-input layui-btn-danger" value="错误" readonly >\n' +
                            '    </div>\n' +
                            '</div>';
                    }else if(data[i].result==1){
                        html+='<div class="layui-form-item">\n' +
                            '    <label class="layui-form-label">结果</label>\n' +
                            '    <div class="layui-input-block">\n' +
                            '        <span class="layui-badge layui-bg-green">正确</span>\n' +
                            '    </div>\n' +
                            '</div>';
                    }else if(data[i].result==3){
                        html+='<div class="layui-form-item">\n' +
                            '    <label class="layui-form-label">结果</label>\n' +
                            '    <div class="layui-input-block">\n' +
                            '        <span class="layui-badge layui-bg-orange">半对</span>\n' +
                            '    </div>\n' +
                            '</div>';
                    }
                       html+='<div class="layui-form-item layui-bg-gray">\n' +
                        '    <label class="layui-form-label">参考答案</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '        <textarea  class="layui-textarea layui-bg-gray"  autoHeight="true" readonly>'+data[i].standard+'</textarea>\n' +
                        '    </div>\n' +
                        '</div>';
                    num++;
                    flag=false;
                }
            }
            if(flag){
                html='';
            }
            return html;
        }
    });


    /**
     * textarea高度自适应
     */
    $(function(){
        $.fn.autoHeight = function(){
            function autoHeight(elem){
                elem.style.height = 'auto';
                elem.scrollTop = 0; //防抖动
                elem.style.height = elem.scrollHeight +5+ 'px';
            }
            this.each(function(){
                autoHeight(this);
                $(this).on('keyup', function(){
                    autoHeight(this);
                });
            });
        }
        $('textarea[autoHeight]').autoHeight();
    })
</script>